package com.sdehl.kafkatransfer.kafka.producer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sdehl.kafkatransfer.constant.KaKouTableList;
import com.sdehl.kafkatransfer.constant.KafkaConst;
import com.sdehl.kafkatransfer.constant.enums.DriveDirectionEnum;
import com.sdehl.kafkatransfer.constant.enums.PlateColorEnum;
import com.sdehl.kafkatransfer.constant.enums.VehicleColorEnum;
import com.sdehl.kafkatransfer.entity.imageAnalyse.*;
import com.sdehl.kafkatransfer.entity.passcar.KaKouInfo;
import com.sdehl.kafkatransfer.entity.passcar.PassCarInfo;
import com.sdehl.kafkatransfer.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Jiax
 * @version 1.0.0
 * @description: KafkaProducer
 * @date 2023-10-12 17:39
 */
@Slf4j
@Component
public class MyKafkaProducer {
    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    /**
     * 发送数据
     */
    public void send(PassCarInfo passCarInfo){
        // fixme 数据转换构建发送数据
        ImageAnalyse imageAnalyse = buildImageAnalyse(passCarInfo);
        log.info("发送数据到kafka,topic:{},内容：{}" ,KafkaConst.PRODUCER_TOPIC, JSONObject.toJSONString(imageAnalyse));

        //发送String
        //kafkaTemplate.send("test", JSON.toJSONString(imageAnalyse));

        //发送byte[]
        kafkaTemplate.send(new ProducerRecord(KafkaConst.PRODUCER_TOPIC,JSON.toJSONString(imageAnalyse).getBytes()));
    }

    /**
     * 构建发送数据
     * @return
     */
    private ImageAnalyse buildImageAnalyse(PassCarInfo passCarInfo) {
        ImageAnalyse imageAnalyse = new ImageAnalyse();
        imageAnalyse.setEventType("vehicleRcogResult");
        imageAnalyse.setEventDescription("vehicleRcogResult");

        List<VehicleRcogResult> vehicleRcogResultList = new ArrayList<>();
        VehicleRcogResult vehicleRcogResult = new VehicleRcogResult();
        //目标属性列表
        TargetAttrs targetAttrs = new TargetAttrs();
        targetAttrs.setPassTime(DateUtil.getUTCDateStr(passCarInfo.getPASSTIME()));
        targetAttrs.setVehiclePicNum(passCarInfo.getPictureNum());
        targetAttrs.setPlatePicUrl(passCarInfo.getIMGID3());
        targetAttrs.setVehiclePicUrl2(passCarInfo.getIMGID1());
        targetAttrs.setVehiclePicUrl3(passCarInfo.getIMGID2());
        // fixme 需要转换
        KaKouInfo kouInfo = KaKouTableList.getKaKouInfoBy4Code(passCarInfo.getTGSID());
        targetAttrs.setCrossingId(kouInfo.getId());
        targetAttrs.setCrossingIndexCode(kouInfo.getIndexCode());

        targetAttrs.setLaneNo(passCarInfo.getDRIVEWAY());
        targetAttrs.setVehicleSpeed(passCarInfo.getSPEED());
        //行驶方向（必须）
        targetAttrs.setDirectionIndex(DriveDirectionEnum.getNameByCode(passCarInfo.getDRIVEDIR()));
        vehicleRcogResult.setTargetAttrs(targetAttrs);

        //目标信息
        List<TargetData> targetList = new ArrayList<>();
        TargetData targetData = new TargetData();
        VehicleData vehicleData = new VehicleData();
        vehicleData.setPlateNo(new TargetValueData(passCarInfo.getCARPLATE()));
        // 号牌种类（对不上，不传了）
        //vehicleTarget.setPlateType(new TargetValueData(passCarInfo.getPLATETYPE()));
        // 车牌颜色（必须）
        vehicleData.setPlateColor(new TargetValueData(PlateColorEnum.getEnNameByCode(passCarInfo.getPLATECOLOR())));
        // 车身颜色
        //vehicleData.setVehicleColor(new TargetValueData(VehicleColorEnum.getEnNameByCode(passCarInfo.getCARCOLOR())));
        //车辆主品牌
        vehicleData.setVehicleLogo(new TargetValueData(passCarInfo.getCARBRANDZW()));
        targetData.setVehicle(vehicleData);
        targetData.setAnalysis_status("1");
        targetList.add(targetData);

        vehicleRcogResult.setTarget(targetList);

        vehicleRcogResultList.add(vehicleRcogResult);
        imageAnalyse.setVehicleRcogResult(vehicleRcogResultList);
        return imageAnalyse;
    }

}
